<?php
// 党务村务公开
namespace app\home\controller;
use think\Db;
class Jcopen extends HomeBase {
	//显示左侧三务公开的名称及所有三务数据

	public function getlist() {

		$map = array();
		$catid = input('catid','0','intval');		// 分类
		$order = input('order');
		if(!$order){
			$order='id DESC';
		}// 排序
		$page = input('page','1','intval');			// 当前页
		$row = input('row','10','intval');			// 每页显示数据


		// 查询条件
		if ($catid = intval($catid)) $map['catid'] = $catid;

		// 排序
		if (empty($order)) $order = 'sort asc, create_time desc';

		// 分页
		$start = ($page-1) * $row;

		$list = array();
		if (!empty($map)) {
			$total = Db::name('Jcopen')->where($map)->count('id');
			$list = Db::name('Jcopen')->where($map)->order($order)->limit($start,$row)->field('id,title,catid,bsid')->select();
		} else {
			$total = Db::name('Jcopen')->count('id');
			$list = Db::name('Jcopen')->order($order)->limit($start,$row)->field('id,title,catid,bsid')->select();
		}
		if (empty($list))
			return json(array('status'=>0,'msg'=>'暂无数据'));
		else {
			foreach ($list as &$value) {
				$cat = Db::name('Cat')->where(array('id'=>$value['catid'],'mark'=>'dangwu'))->field('title')->find();
				$value['cat'] = $cat['title'];

			}
			return json(array('status'=>1,'info'=>$list,'count'=>$total));
		}
	}

    //每条信息的详情信息
	public function getdetail() {
		$id = input('id');
		$id = intval($id);
		$info = Db::name('Jcopen')->where('catid',$id)->find();
		if (!$info)
			return json(array('status'=>0,'msg'=>'数据未找到'));
		$town = Db::name('Towns')->where('id',$info['townsid'])->field('tname')->find();
		$info['town_name'] = $town['tname'];
		return json(array('status'=>1,'info'=>$info));
	}
    //右侧列表
	public function jcopenlist() {
		$map = [];
		$title = input('title');
		if ($title) {
			$map[] = ['title', 'like', '%'.$title.'%'];
		}
		$catid = input('catid');
		if ($catid) {
			$map[] = ['catid', '=', $catid];
		}

		$bsid = input('bsid');
		if ($bsid) {
			$map[] = ['bsid', '=', $bsid];
		}

		$townsid = input('townsid');

		if ($townsid) {
			$map[] = ['townsid', '=', $townsid];
		}


		$current = input('current');
		if (!$current) {
			$current = 1;
		}
		$pagenums = input('pagenums');
		if (!$pagenums) {
			$pagenums = 50;
		}
		$nowpagenums = ($current - 1) * $pagenums;
		$order = input('order');
		if (!$order) {
			$order = 'id DESC';
		}
		if ($map) {
			$total = Db::name('Jcopen')->where($map)->count('id');
			$info = Db::name('Jcopen')->where($map)->field('id,title,update_time,townsid,bsid,content')->order($order)->limit($nowpagenums, $pagenums)->select();
		} else {
			$total = Db::name('Jcopen')->count('id');
			$info = Db::name('Jcopen')->order($order)->field('id,title,update_time,townsid,bsid,content')->limit($nowpagenums, $pagenums)->select();
		}
		if ($info) {
			foreach ($info as $key => $value) {
				$info[$key]['update_time'] = time_nianyueri($value['update_time']);
				$info[$key]['tname'] = $this->get_alltowns($value['townsid']);
				$info[$key]['bsidtitle'] = Db::name('Cat')->where('id', $value['bsid'])->value('title');
			}
			$datas['status'] = 1;
			$datas['info'] = $info;
			$datas['total'] = $total;
			$datas['current'] = $current;
		} else {
			$datas['status'] = 0;
			$datas['info'] = '无数据！';
			$datas['total'] = $total;
			$datas['current'] = $current;
		}

		return json($datas);
	}
	/*
 * 获取村镇信息 主要是一级分类及其下属分类
 * pid
 */
	function get_alltowns($code){
		if($code){
			$info=Db::name('Towns')->where('code',$code)->find();
			if($info){
				$infos=Db::name('Towns')->where('code',$info['pid'])->value('tname');
				return $infos.'-'.$info['tname'];
			}else{
				return 'error';
			}
		}else{
			return 'canshuerror';
		}
	}
}